package com.ge.med.cchelper.listener;

import java.io.File;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.structuremergeviewer.DiffNode;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.ge.med.cchelper.bean.CSession;
import com.ge.med.cchelper.bean.Response;
import com.ge.med.cchelper.popup.job.CompareSource;
import com.ge.med.cchelper.util.LogUtil;
import com.ge.med.cchelper.util.ReourceHelper;
import com.ge.med.cchelper.util.SSLhelper;
import com.ge.med.cchelper.util.ftp.FTPFactory;
import com.ge.med.cchelper.util.ftp.FtpHelper;

public class CompareListener implements Listener {
	CSession csession;
	Shell dialogShell;
	Text remotePathT;
	String sourceTempDir;
	String fileName;
	public String localFilePath;
	public String localCompareFile;
	String remoteFile;
	
	Text passwordt;
	Text viewNamet;
	Text patht;
	Text cleartoolt;
	

	public CompareListener(Shell dialogShell, Text remotePathT, String localFilePath,String sourceTempDir,String fileName) {
		this.dialogShell = dialogShell;
		this.remotePathT = remotePathT;
		this.localFilePath = localFilePath;
		this.sourceTempDir = sourceTempDir;
		this.fileName = fileName;
		localCompareFile = localFilePath+".compare";
		ReourceHelper resource = ReourceHelper.getResource();
		IProject project = ReourceHelper.getProject();
		csession = resource.getSession(project.getName());
	}

	public void handleEvent(Event arg0) {
	
		if(csession==null){
			return;
		}
		
	    remoteFile = remotePathT.getText();
		
		Job compare = new Job(""){
			
			protected IStatus run(IProgressMonitor arg0) {
				
				//copy compare file to temp dir.
				SSLhelper sslHelper = new SSLhelper(csession);
				Response res = sslHelper.executeCT("cp -rf " + remoteFile
						+ " " + sourceTempDir+"/"+fileName);
				int status = res.getStatus();
				if(status==0){
					FtpHelper ftp = null;
					try {
						ftp = FTPFactory.getConnectionInstance(csession.getHost(), ".");
						if (ftp.downloadPlus(
								sourceTempDir + "/" + fileName,localCompareFile,arg0,false)) {
							sslHelper.execute("rm -rf " + sourceTempDir
									+ "/" + fileName);

							Display.getDefault().syncExec(new Runnable(){
	
								public void run() {
									compareFile();
									
								}});
							
							File file = new File(localCompareFile);
							if(file.exists()){
								file.delete();
							}
						}
					} catch (Exception e) {
						e.printStackTrace();
						LogUtil.err(e.getMessage());
						return Status.CANCEL_STATUS;
					}
				}
				
				
				return Status.OK_STATUS;
			}
			
		};
		
		compare.schedule();
    	
	}
	
	public void compareFile(){
		CompareConfiguration config = new CompareConfiguration();  
    	config.setProperty(CompareConfiguration.SHOW_PSEUDO_CONFLICTS, Boolean.FALSE);  
    	  
    	// left  
    	config.setLeftEditable(true);  
    	config.setLeftLabel("Local:"+fileName);  
    	  
    	// right  
    	config.setRightEditable(false);  
    	String remoteFile = remotePathT.getText();
    	config.setRightLabel("Remote:"+remoteFile);  
    	
    	CompareEditorInput editorInput = new CompareEditorInput(config) {  
    		CompareSource left = new CompareSource(localFilePath);  
    		CompareSource right = new CompareSource(localCompareFile);  
    	      
    	      
    	    protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
    	        return new DiffNode(left, right);  
    	    }  
    	  
    	      
    	    public void saveChanges(IProgressMonitor pm) throws CoreException {  
    	        super.saveChanges(pm);  
    	        left.writeFile();  
    	       // right.writeFile();  
    	    }  
    	};  
    	  
    	editorInput.setTitle("Source Compare");
    	
    	//CompareUI.openCompareEditor(editorInput); 
    	  
    	CompareUI.openCompareDialog(editorInput); 
    	
	}

}
